UpdateCustomerCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 24
dl 0
loc 27
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 19 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { UpdateCustomerCommand } from './UpdateCustomerCommand';
4
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository';
5
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException';
6
import { IsCustomerAlreadyExist } from 'src/Domain/Customer/Specification/IsCustomerAlreadyExist';
7
import { CustomerAlreadyExistException } from 'src/Domain/Customer/Exception/CustomerAlreadyExistException';
8
9
@CommandHandler(UpdateCustomerCommand)
10
export class UpdateCustomerCommandHandler {
11
  constructor(
12
    @Inject('ICustomerRepository')
13
    private readonly customerRepository: ICustomerRepository,
14
    private readonly isCustomerAlreadyExist: IsCustomerAlreadyExist
15
  ) {}
16
17
  public async execute(command: UpdateCustomerCommand): Promise<void> {
18
    const { id, name } = command;
19
20
    const customer = await this.customerRepository.findOneById(id);
21
    if (!customer) {
22
      throw new CustomerNotFoundException();
23
    }
24
25
    if (
26
      name !== customer.getName() &&
27
      true === (await this.isCustomerAlreadyExist.isSatisfiedBy(name))
28
    ) {
29
      throw new CustomerAlreadyExistException();
30
    }
31
32
    customer.updateName(name);
33
34
    await this.customerRepository.save(customer);
35
  }
36
}
37